<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Reg extends MY_Controller {
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
	}
	
	/**
	 * 用户注册
	 */
	function index(){
		
		if($this->input->post('act') == 'reg'){
			$ret = array(
				'msg' => '',
				'sycDz' => '',
				'data' => array(
					'url' => '',
				),
			);
			
			$this->load->model('User_model', 'user');
//			$invite = intval($this->input->post('invite'));
            $invite = intval($this->input->post('invite_userid'));
			//$invite_info = $this->user->get_one(array('user_id'=>$invite), 'user_id');
			if(strlen($invite) == 11){
				$invite_info = $this->user->get_one(array('phone'=>$invite),'user_id');
				$userId = $invite_info['user_id'];
				$inviteUid = !empty($userId) ? $userId : 0 ;
			}else $inviteUid = 0;
			$data = array(
				'phone'      => trim($this->input->post('phone')),
                'username'      => trim($this->input->post('username')),
				'password'		=> md5(md5($this->input->post('password'))),
				'invite_userid' => $inviteUid,
				'email_status'  => '0',
				'type_id'       => '2',
				'add_time'      => time(),
				'addip'         => $this->input->ip_address(),
			);
			
			if ($data['username']==""){
				$ret['msg'] = '用户名不能为空';
				echo json_encode($ret);exit;
			}elseif(!isset($data['username'][2])){
				$ret['msg'] = '用户名太短';
				echo json_encode($ret);exit;
			}elseif(!$this->myformcheck->isName($data['username'])){
				$ret['msg'] = '无效的用户名';
				echo json_encode($ret);exit;
            }elseif ($this->input->post('password')==""){
				$ret['msg'] = '密码不能为空';
				echo json_encode($ret);exit;
			}elseif($this->input->post('password') != $this->input->post('password2')){
				$ret['msg'] = '两次密码不一致';
				echo json_encode($ret);exit;
			}elseif(!$this->myformcheck->isPhone($data['phone'])){
				$ret['msg'] = '请填写有效的手机号码';
				echo json_encode($ret);exit;
			}
			
			$this->load->model('User_model', 'user');
			if($this->user->get_one(array('username'=>$data['username']), 'user_id')){
				$ret['msg'] = '用户名已存在';
				echo json_encode($ret);exit;
			}

            $this->load->model('User_model', 'user');
			if($this->user->get_one(array('phone'=>$data['phone']), 'user_id')){
				$ret['msg'] = '手机号码已存在';
				echo json_encode($ret);exit;
			}
					
			//手机验证码
						
			$verify_code = $this->input->post('verify_code');
			$md5 = $this->session->userdata('zjy_temp_userid');
			if($verify_code == $md5){
				$data['phone_status'] = 1;
			}else{
				$ret['msg'] = '手机验证码不正确';
				echo json_encode($ret);
				exit;
			}
			$data['phone_status'] = 1;
//			$data['username'] = iconv('GBK', 'UTF-8', $data['username']);
			//添加注册信息到用户表
			$user_id = $this->user->add($data);
			if($user_id){
                //添加体验金
                $this->load->model('Account_model');
                $initAccount = array(
                    'user_id' => $user_id,
                    'total' => '0',
                    'use_money' => '0',
                    'no_use_money' => '0',
                    'collection' => '0',
                    'reg_money'=>1000
                );
                $this->Account_model->init_account($initAccount);
				
				//发送注册邮件				
//				$this->Notice(1, $user_id);

                //发送注册成功站内信
                $this->load->model('Message_model');
                $this->Message_model->send(array('sent_user' => 1, 'receive_user' => $user_id,
                    'name' => "注册成功",
                    'fstatus' => 0,
                    'message_type' => 'system',
                    'sented' => 0,
                    'deltype' => 0,
                    'content' => "恭喜您注册成功，祝您投资愉快！【华银资本】",
                    'add_time' => time(),
                    'addip' => $this->js->get_ip()
                ));

				//添加积分日志并修改积分
				$this->Credit($user_id,'reg');		
					
				//手机积分日志
				$this->Credit($user_id,'phone');
				
				//注册成功自动登陆
				$user = array(
					'user_id' => $user_id,
					'username' => $data['username'],
                    'phone' => $data['phone'],
					'email' => $data['email'],
					'login_time' => time()
				);
				$this->session->set_userdata('zjy_user',$user);

				$ret['data']['url'] = '/user/center.html?s=r';
				echo json_encode($ret);exit;
			}else{
				$ret['msg'] = '注册失败';
				echo json_encode($ret);exit;
			}
			
		}else{
			
			if (isset($_COOKIE['inviteRequest']) && $_COOKIE['inviteRequest']){
					$inviteRequest = $_COOKIE['inviteRequest'];
					$data['invite'] = $inviteRequest;
			}
			
			$data['title'] = "用户注册".' - '.CON_KEYWORDS;
			$this->load->view('user/user_reg',$data);
		}
	}
	
	/**
	 * 注册邮箱激活
	 */
	function activation(){
		$this->load->model('User_model', 'user');
		
		//获取加密字符串并解密
		$this->load->library('func');
		$act = $this->input->get('act');
		$str = $this->func->authcode($act,"DECODE");
		$arr = explode(',', $str);
		if(count($arr)==3){
			$user_id = $arr[0];
			$username= $arr[1];
			$email   = $arr[2];
			$row = $this->user->get_one(array('user_id'=>$user_id),'username,email,email_status');
			if($row && $row['username'] == $username && $row['email'] == $email){
				if($row['email_status'] != 1){
					$update = array(
						'email_status' => 1
					);
					$res = $this->user->update($update,array('user_id'=>$user_id));
					if($res) {
						//添加积分日志并修改积分
						$this->Credit($user_id,'email');						
						
						$data['succ'] = '1';
						$data['msg'] = '邮箱认证成功';
					}
				}else{
					$data['msg'] = '该邮箱账户已认证';
				}
				
			}else{
				$data['msg'] = '错误数据';
			}
		}else{
			$data['msg'] = '错误数据';
		}
		
		$this->load->view('user/user_email',$data);
		//echo "<script>alert('".$data['msg']."');location='/user/center';</script>";
	}
	
	public function apply_phone($id = ''){
		$this->load->model('User_model', 'user');
		$ret = array(
			'msg' => '',
			'url' => '',
		);

		$k = md5('ychdx');
		if(isset($_COOKIE[$k]) && $_COOKIE[$k] == 1){
			$ret['msg'] = '60秒内只能获取一次短信验证码，请稍后再试';
			echo json_encode($ret); 
			exit;				
		}else{
			$phone = $this->input->post('phone');
			if(!$phone){
				$ret['msg'] = '请填写手机号码';
			}elseif(!$this->myformcheck->isPhone($phone)){
				$ret['msg'] = '请填写有效的手机号码';
			}else{

                if($id == ''){//如果不是找回登录密码的要验证手机重复
                    // 验证手机号码是否存在
                    $tmpwhere = array(
                        'phone'=>$phone,
                    );
                    $row = $this->user->get_one($tmpwhere,'user_id');
                    if(isset($row['user_id'])){
                        $ret['msg'] = '该手机号码已存在';
                    }
                }
			}
			if($ret['msg']){
				echo json_encode($ret); 
				exit;				
			}
			
			$md5 = rand_string($phone,6,1,13);

            if($id == 12){
                $res = $this->Notice(12, 0, array('vcode'=>$md5,'phone'=>$phone));
                $this->session->set_userdata('zjy_temp_captcha',$md5);
            } else {
                $res = $this->Notice(4, 0, array('vcode'=>$md5,'phone'=>$phone));
                $this->session->set_userdata('zjy_temp_userid',$md5);
            }

			
			if($res){
				$ctime = time()+60;//60秒内只能发送一次短信息
				setcookie($k,1,$ctime);
				
				$ret['msg'] = '手机验证码已发送，请注意查收';


				$ret['info'] = '1';
				echo json_encode($ret); 
				exit;
			}else{
				$ret['msg'] = '短信发送失败，请稍后再试或联系客服';
				echo json_encode($ret); 
				exit;
			}
			
			echo json_encode($ret); 
			exit;
			
		}
		
	}
	
}
